package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/community")
public class CommunityController extends BaseController {
    @DubboReference
    private CommunityService communityService;

    @DubboReference
    private DictService dictService;

    /*渲染index视图的处理器*/
    @PreAuthorize("hasAuthority('community.show')")
    @RequestMapping
    public String index(HttpServletRequest request, ModelMap modelMap){
        //  分页查询的条件
        Map<String, Object> filters = getFilters(request);

        // 将分页查询的条件放入请求域
        modelMap.addAttribute("filters",filters);
        // 调用分页查询的方法,将查询的数据放入请求域
        PageInfo<Community> page = communityService.findPage(filters);
        modelMap.addAttribute("page",page);
        // 查询北京下所有区域,获取List<Dict> areaList
        List<Dict> areaList= dictService.findListByDictCode("beijing");
        // 将areaList放入请求域
        modelMap.addAttribute("areaList",areaList);

        return "community/index";
    }


    /*
    * 渲染小区新增页面 的处理器
    *
    *
    * */
    @PreAuthorize("hasAuthority('community.create')")
    @RequestMapping("/create")
    public String create(ModelMap modelMap){
        // 查询北京下的所有区域,将数据放入请求域
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        modelMap.addAttribute("areaList",areaList);
        return "community/create";
    }

    /*定义一个存储新增Cummnuity数据进入数据的处理器
    * 渲染成功提示页
    * */
    @PreAuthorize("hasAuthority('community.create')")
    @RequestMapping("/save")
    public String save(Community community){
        // 调用服务层方法存入数据库
        communityService.insert(community);
        // 渲染成功页
        return "common/successPage";
    }

    /* 渲染小区信息修改页的处理器*/
    @PreAuthorize("hasAuthority('community.edit')")
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id")Long id,ModelMap modelMap){
        // 调用服务层方法 根据id查询小区信息
        Community community = communityService.getById(id);
        // 将小区信息放入请求域
        modelMap.addAttribute("community",community);
        // 查询北京的所有区域放入请求域
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        modelMap.addAttribute("areaList",areaList);

        return "community/edit";

    }

    /*接收前端提交过来的要更新的小区信息,
    * 更新成功后渲染成功提示页
    * */
    @PreAuthorize("hasAuthority('community.edit')")
    @RequestMapping("/update")
    public String update(Community community){
        communityService.update(community);
        return "common/successPage";

    }

    /*删除小区的处理器  删除完毕后,重定向 /community  */
    @PreAuthorize("hasAuthority('community.delete')")
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id){
        communityService.delete(id);
        return "redirect:/community";
    }


}
